package com.orange.util;

/**
 * Created by pengfei on 16/6/11.
 */

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class SecretUtil {
    private static String aesKey = "bf33b0bbc467fe47d39507efd95f02b22015-96";
    private static String secretType = "AES";
    private static int aesSize = 128;
    private static Cipher aesEncryptCipher = null;
    private static Cipher aesDecryptCipher = null;

    static {
        try {
            Key key = getAESKey(aesKey.getBytes());
            aesEncryptCipher = Cipher.getInstance(secretType);
            aesEncryptCipher.init(Cipher.ENCRYPT_MODE, key);
            aesDecryptCipher = Cipher.getInstance(secretType);
            aesDecryptCipher.init(Cipher.DECRYPT_MODE, key);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static byte[] aesEncrypt(byte[] arrB)
            throws Exception {
        return aesEncryptCipher.doFinal(arrB);
    }

    public static String aesEncryptMode(String strIn)
            throws Exception {
        return SecretUtil.byteArr2HexStr(aesEncrypt(strIn.getBytes()));
    }

    public static byte[] aesDecrypt(byte[] arrB)
            throws Exception {
        return aesDecryptCipher.doFinal(arrB);
    }

    public static String aesDecryptMode(String strIn)
            throws Exception {
        try {
            return new String(aesDecrypt(SecretUtil.hexStr2ByteArr(strIn)));
        } catch (Exception e) {
            throw new Exception("解密失败");
        }
    }

    private static Key getAESKey(byte[] arrBTmp)
            throws NoSuchAlgorithmException {
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(arrBTmp);

        keygen.init(aesSize, random);
        return keygen.generateKey();
    }

    public static String byteArr2HexStr(byte[] arrB)
            throws Exception {
        int iLen = arrB.length;

        StringBuffer sb = new StringBuffer(iLen * 2);
        for (int i = 0; i < iLen; ++i) {
            int intTmp = arrB[i];

            while (intTmp < 0) {
                intTmp += 256;
            }

            if (intTmp < 16)
                sb.append("0");

            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
    }

    public static byte[] hexStr2ByteArr(String strIn)
            throws Exception {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;

        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i += 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[(i / 2)] = (byte) Integer.parseInt(strTmp, 16);
        }
        return arrOut;
    }

    public static void main(String[] args) {
        String szSrc = "fa7cc03fd99604c185b497256c9c8d82";
        System.out.println("加密前的字符串:" + "");
        try {
            String src = aesEncryptMode(szSrc);
            System.out.println("加密后的字符串:" + src);
//            String text = aesDecryptMode("a75522b3ee4e5271e598515e54fb4880");
//            System.out.println("解密后的字符串:" + text);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
